﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Net;
using SettLib;

namespace Configurator
{
    public partial class frmMain : Form
    {
        
        public cSettings Settings;
        public frmMain()
        {
            Settings = new cSettings();
            Settings.LoadSettings(Path.Combine(Application.StartupPath, "settings.xml"));
            InitializeComponent();            
            //main settings
            tbSDFFilename.Text = Settings.Data.Main.SDFFilename;
            //syslog settings
            nudSyslogPort.Value = Settings.Data.Syslog.Port;
            nudQueueProcessing.Value = Settings.Data.Syslog.EventsQueueProcessingPeriod;
            nudCachesCount.Value = Settings.Data.Syslog.CachesCount;
            nudCache.Value = Settings.Data.Syslog.CacheSize;
            nudFlushPeriod.Value = Settings.Data.Syslog.FlushPeriod;
            nudDNSResolvingPeriod.Value = Settings.Data.Syslog.DNSResolvePeriod;
            nudDNSTimeout.Value = Settings.Data.Syslog.DNSResolveTimeout;
            if (Settings.Data.Syslog.Interfaces.Count > 0)
                foreach (object[] IfData in Settings.Data.Syslog.Interfaces)
                {
                    dsInterfaces.Tables[0].Rows.Add(IfData);
                }
            //webserver settings
            nudWebServerPort.Value = Settings.Data.Webserver.Port;
            tbWWWPath.Text = Settings.Data.Webserver.wwwPath;
            nudSessionTimeout.Value = Settings.Data.Webserver.SessionTimeout;
            nudTempDirClearPeriod.Value = Settings.Data.Webserver.TempDirClearPeriod;


        }




        private void button1_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog() == DialogResult.OK)
                tbSDFFilename.Text = ofd.FileName;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
                tbWWWPath.Text = fbd.SelectedPath;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //main
            Settings.Data.Main.SDFFilename = tbSDFFilename.Text;
            //syslog
            Settings.Data.Syslog.Port = (int)nudSyslogPort.Value;
            Settings.Data.Syslog.EventsQueueProcessingPeriod = (int)nudQueueProcessing.Value;
            Settings.Data.Syslog.CachesCount = (int)nudCachesCount.Value;
            Settings.Data.Syslog.CacheSize = (int)nudCache.Value;
            Settings.Data.Syslog.FlushPeriod = (int)nudFlushPeriod.Value;
            Settings.Data.Syslog.DNSResolvePeriod = (int)nudDNSResolvingPeriod.Value;
            Settings.Data.Syslog.DNSResolveTimeout = (int)nudDNSTimeout.Value;
            Settings.Data.Syslog.Interfaces.Clear();
            if (dsInterfaces.Tables[0].Rows.Count > 0)
                foreach (DataRow IfRow in dsInterfaces.Tables[0].Rows)
                {
                    if ((IfRow[0].ToString() == string.Empty) || (IfRow[1].ToString() == string.Empty))
                    {
                        MessageBox.Show("В таблице интерфейсов пустые значения не допускаются", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    object[] IfData = new object[2];
                    IfData = IfRow.ItemArray;
                    Settings.Data.Syslog.Interfaces.Add(IfData);
                }
            //webserver
            Settings.Data.Webserver.Port = (int)nudWebServerPort.Value;
            Settings.Data.Webserver.wwwPath = tbWWWPath.Text;
            Settings.Data.Webserver.SessionTimeout = (int)nudSessionTimeout.Value;
            Settings.Data.Webserver.TempDirClearPeriod = (int)nudTempDirClearPeriod.Value;
            
            
            if (Settings.SaveSettings(Path.Combine(Application.StartupPath, "settings.xml")))
            {
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    File.Copy(Path.Combine(Application.StartupPath, "settings.xml"), sfd.FileName, true);
                }
            }
            else
            {
                MessageBox.Show("Не удалось сохранить");
            }
        }

        private void dgvInterfaces_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {            
            if (e.FormattedValue.ToString() != string.Empty)
            if (e.ColumnIndex == 0)
            {
                try
                {
                    IPAddress.Parse(e.FormattedValue.ToString());
                }
                catch
                {
                    e.Cancel = true;
                }                    
            }
        }
    }
}